<?php

    //9/3/2009: Immanuel192 fix this action

    if (!defined('EXPONENT')) exit('');

    $i18n = exponent_lang_loadFile('modules/contactmodule/actions/contact.php');

    $template = new template('contactmodule',$_POST['msg'],$loc);
    // make current datetime format
    $datetext = date('l jS \of F Y h:i:s A');
    $_POST['date_text'] = $datetext;
    $template->assign('post',$_POST);

    $msg = $template->render();

    $config = $db->selectObject('contactmodule_config',"location_data='".serialize($loc)."'");
    
    // for mobile, we will not verify capcha
    global $browser;
    if ($browser->isMobile())
        unset($config->use_captcha);
    
    if (!empty($config->use_captcha)) $validate['captcha'] = 'captcha_string';
    $validate['valid_email'] = 'email';
    if (count($validate) > 0) validator::validate($validate, $_POST);
    if ($config == null) {
        $config->subject = $i18n['default_subject'];
        $config->replyto_address = '';
        $config->from_address = 'info@'.HOSTNAME;
        $config->from_name = $i18n['default_from'];

    } else {
        if ($config->subject == '') {
            $config->subject = $i18n['default_subject'];
        }
    }
    exponent_sessions_unset("captcha_string");
    $headers = array();
    // 9/3/2009 : immanuel192 fix this; 
    // for FROM field: FROM what? FROM website delivery system
    $headers['From'] =  SMTP_FROMADDRESS ;
    $headers['Reply-to'] = $_POST['email'];

    /*if (isset($_POST['email']) && $_POST['email'] != '') {
    $headers['From'] = isset($_POST['name']) ? $_POST['name'] . ' <'.$_POST['email'].'>' : $_POST['email'];
    $headers['Reply-to'] = $_POST['email'];
    } else {
    $headers['From'] = $config->from_name . ' <'.$config->from_address.'>';
    $headers['Reply-to'] = $config->replyto_address;

    }
    */

    if (!defined('SYS_USERS')) include_once(BASE.'subsystems/users.php');

    $emails = array();
    foreach ($db->selectObjects('contact_contact',"location_data='".serialize($loc)."'") as $c) {
        if ($c->user_id != 0) {
            $u = exponent_users_getUserById($c->user_id);
            $emails[] = $u->email;
        } else if ($c->email != '') {
                $emails[] = $c->email;
            }
    }
    require_once(BASE.'subsystems/mail.php');
    $mail = null;

    try
    {
        $mail = new exponentMail();
        $name = $_POST['first_name'] . $_POST['last_name'];
    
        $mail->addTo($emails);
        $mail->subject($config->subject);
        $mail->addHTML($msg);
        $mail->addHeaders($headers);
        $mail->addFrom($_POST['email'], $name );
        
        try
        {
            // Send the message
            if (@$mail->send() > 0)
            {
                $template = new template('contactmodule','_final_message');
                $template->assign('message',$config->final_message);
                $template->output();
            }
        }
        catch (Exception $e)
        {
//        die("ERROR: SMTP Configure error. Please re-configure.");
            
            validator::failAndReturnToForm($i18n['error_email_wrong']); 
        }
        
    }
    catch (Exception $e)
    {
        die("ERROR: SMTP Configure error. Please re-configure.");
        echo "<script language='javascript'>
            setTimeout(function(){
               window.location='index.php';
            }, 5000);
        </script>";
    }
    
   //print_r($mail);
     //else {
    //echo $i18n['smtp_error'];
    //}
?>
